<script>
  import {
    checkVersion
  } from '@/api/index.js'
  export default {
    onLaunch: function() {
      let token = this.$ui.getStorage('token') || 'none'
      if (token == 'none') {
        //存在则关闭启动页进入首页
        // #ifdef APP-PLUS
        plus.navigator.closeSplashscreen();
        // #endif
      } else {
        uni.reLaunch({
          url: "/pages/home/home",
          success: () => {
            // #ifdef APP-PLUS
            plus.navigator.closeSplashscreen();
            // #endif
          }
        })
      }
      // #ifdef APP-PLUS  
      plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) { //获取本机app版本号应用名等相关信息
        checkVersion().then(res => {
          const newVer = res.data.app_v
          const oldVer = widgetInfo.version
          console.log(newVer);
          if (newVer != oldVer) {
            uni.showToast({
              title: '发现新版本，后台静默下载，下载完自动重启',
              icon: 'none'
            })
            uni.downloadFile({ //开始下载后端下发wgt包
              url: res.data.app_wgt_url,
              success: (downloadResult) => {
                if (downloadResult.statusCode == 200) {
                  console.log(1);
                  plus.runtime.install(downloadResult.tempFilePath, { //进行自动安装
                    force: false
                  }, function() {
                    console.log(2);
                    console.log('install success...');
                    plus.runtime.restart(); //安装成功后重启应用
                  }, function(e) {
                    console.error('install fail...');
                  });
                }
              },
              fail(err) {
                console.log(err, 123);
                uni.showToast({
                  title: '下载失败',
                  icon: 'none'
                })
              },
            });
          }
        })
      });
      // #endif

      const version = '1.0.0'
      console.log(`%c 版本号 %c v${version} `,
        'background:#35495e ; padding: 1px; border-radius: 3px 0 0 3px;  color: #fff',
        'background:#007AFF ;padding: 1px; border-radius: 0 3px 3px 0;  color: #fff; font-weight: bold;')
    },
    onShow: function() {
      console.log('App Show')
    },
    onHide: function() {
      console.log('App Hide')
    }
  }
</script>

<style lang="less">
  .uni-modal {
    border-radius: 30rpx !important;
  }

  /*每个页面公共css */
  @import url("./static/style/index.less");
  @import url("@/utils/common.css");
</style>

<style lang="scss">
  .uni-modal {
    border-radius: 30rpx !important;
  }

  @import "@/uni_modules/uview-ui/index.scss";
</style>
